package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Position;
import com.ruoyi.system.service.IPositionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-11-09
 */
@RestController
@RequestMapping("/system/position")
public class PositionController extends BaseController
{
    @Resource
    private IPositionService positionService;

//    @Resource
//    private SysPermissionService permissionService;

    @Resource
    private ISysUserService sysUserService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:position:list')")
    @GetMapping("/list")
    public TableDataInfo list(Position position)
    {
        startPage();
        List<Position> list = positionService.selectPositionList(position);
        return getDataTable(list);
    }

    @GetMapping("/assignList")
    public TableDataInfo assignList(Position position)
    {
        startPage();
        //查询当前用户角色
//        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        if (roles != null && roles.size() > 0) {
//            if (roles.contains("admin") || roles.contains("boss") ||
//                    roles.contains("assign")) {
//                position.setAssignedUserId(null);
//            } else {
//                position.setAssignedUserId(String.valueOf(getUserId()));
//            }
//        } else {
//            position.setAssignedUserId(String.valueOf(getUserId()));
//        }
        List<SysUser> sysUserList = sysUserService.selectUserList(new SysUser());
        String uidStr = "";
        if (sysUserList != null && sysUserList.size() > 0) {
            List<String> uidList =
                    sysUserList.stream().map(item -> {
                        Long cid = item.getUserId();
                        return String.valueOf(cid.longValue());
                    }).collect(Collectors.toList());
            uidStr = String.join(",", uidList);
        }
        position.setUidStr(uidStr);
        List<Position> list = positionService.selectPositionList(position);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:position:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Position position)
    {
        List<Position> list = positionService.selectPositionList(position);
        ExcelUtil<Position> util = new ExcelUtil<Position>(Position.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:position:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(positionService.selectPositionById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:position:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Position position)
    {
        position.setCreateTime(new Date());
        position.setCreateUserId(String.valueOf(getUserId()));
        return toAjax(positionService.insertPosition(position));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:position:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Position position)
    {
        return toAjax(positionService.updatePosition(position));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:position:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(positionService.deletePositionByIds(ids));
    }
}
